package com.huawei.esdk.tp.device.smc.v100r003c00;

import java.util.List;

import com.huawei.esdk.platform.common.SDKErrorCode;
import com.huawei.esdk.platform.common.SDKResult;
import com.huawei.esdk.platform.common.config.ConfigManager;
import com.huawei.esdk.platform.common.utils.ApplicationContextUtil;
import com.huawei.esdk.platform.common.utils.StringUtils;
import com.huawei.esdk.platform.commu.itf.ICXFSOAPProtocolAdapter;
import com.huawei.esdk.platform.exception.ProtocolAdapterException;
import com.huawei.esdk.tp.common.ErrInfo;
import com.huawei.esdk.tp.device.smc.v100r002c01.SMC20V1R2C01ConferenceControlCapabilityImpl;
import com.huawei.esdk.tp.device.smc.v100r003c00.SMC20V1R3C00IConferenceControlCapability;
import com.huawei.esdk.tp.device.smc.v100r003c00.convert.TPSMC20V1R3C00ConfMgrConvert;
import com.huawei.esdk.tp.domain.model.RecordingConference;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.ArrayOfSiteVolume;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.ArrayOfString;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.ConferenceId;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.DisplayConfSiteLocalVideo;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.DisplayConfSiteLocalVideoResponse;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.HideConfSiteLocalVideo;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.HideConfSiteLocalVideoResponse;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.SetBroadcast;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.SetBroadcastResponse;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.SetConfSiteVolume;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.SetConfSiteVolumeResponse;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.SetFloor;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.SetFloorResponse;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.SmcExternal2Soap;

/**
 * 会议控制能力类
 * <p>
 * 适配SMC2.0 V1R3C00版本新接口
 * <p>
 * @author  cWX191990
 * @see  [相关类/方法]
 * @since  [eSDK TP V100R003C20]
 */
public class SMC20V1R3C00ConferenceControlCapabilityImpl extends SMC20V1R2C01ConferenceControlCapabilityImpl implements
    SMC20V1R3C00IConferenceControlCapability
{
    
    /**
     * 转换工具类，Spring初始化
     */
    private TPSMC20V1R3C00ConfMgrConvert tpConfMgrConvert =
        ApplicationContextUtil.getBean("tpSMC20V1R3C00ConfMgrConvert");;
    
    public TPSMC20V1R3C00ConfMgrConvert getTpConfMgrConvert()
    {
        return tpConfMgrConvert;
    }
    
    public void setTpConfMgrConvert(TPSMC20V1R3C00ConfMgrConvert tpConfMgrConvert)
    {
        this.tpConfMgrConvert = tpConfMgrConvert;
    }
    
    public SMC20V1R3C00ConferenceControlCapabilityImpl(ICXFSOAPProtocolAdapter cxfSOAPProtocolAdapter)
    {
        super(cxfSOAPProtocolAdapter);
    }
    
    protected String getR3SouthItfName()
    {
        return SmcExternal2Soap.class.getName();
    }
    
    /** 
     * 启动会议直播功能
     *
     * @param confId 会议ID
     * @param videoSourceUri 视频源所在会场标识
     * @return SDKErrorCode 成功：0，失败返回相应错误码
     * @since eSDK TP V100R003C20
     */
    public SDKErrorCode startLiveBroadcastEx(String confId, String videoSourceUri)
    {
        //        SDKErrorCode result = new SDKErrorCode();
        
        //        try
        //        {
        //            southResponse = (CancelBroadcastResponse) sendProtocolMessage(south,getR3SouthItfName(),
        //                    "cancelBroadcast");
        //            result.setErrCode(southResponse.getCancelBroadcastResult().getErrCode());
        //        }
        //        catch (ProtocolAdapterException e)
        //        {
        //            result.setErrCode(e.getErrorCode());
        //            return result;
        //        }
        
        return new SDKErrorCode();
    }
    
    /** 
     * 停止会议直播功能
     *
     * @param confId 会议ID
     * @param videoSourceUri 视频源所在会场标识
     * @return SDKErrorCode 成功：0，失败返回相应错误码
     * @since eSDK TP V100R003C20
     */
    public SDKErrorCode stopLiveBroadcastEx(String confId, String videoSourceUri)
    {
        return new SDKErrorCode();
    }
    
    /** 
     * 启动录制
     *
     * @param confId 会议ID
     * @return SDKErrorCode 成功：0，失败返回相应错误码
     * @since eSDK TP V100R003C20
     */
    public SDKErrorCode startRecordingEx(String confId)
    {
        return new SDKErrorCode();
    }
    
    @Override
    public SDKErrorCode stopRecordingEx(String confId)
    {
        return new SDKErrorCode();
    }
    
    @Override
    public SDKResult<RecordingConference> queryHistoryRecordAddrEx(List<String> confIds)
    {
        SDKResult<RecordingConference> response = new SDKResult<RecordingConference>();
        RecordingConference recordConf = new RecordingConference();
        response.setResult(recordConf);
        return response;
    }
    
    /**
     * 指定会场发言（点名发言）
     * 
     * @param confId 指定的会议ID
     * @param siteUri 指定会场的Uri地址
     * @return SDKErrorCode 0：表示成功  其他数值：表示失败
     * @see eSDK TP V100R003C20
     */
    @Override
    public SDKErrorCode setFloorEx(String confId, String siteUri)
    {
        SDKErrorCode result = new SDKErrorCode();
        SetFloor south = new SetFloor();
        ConferenceId conferenceId = new ConferenceId();
        conferenceId.setConfInternalId(Long.parseLong(confId));
        south.setConfId(conferenceId);
        south.setFloorSite(siteUri);
        
        try
        {
            SetFloorResponse southResponse =
                (SetFloorResponse)sendProtocolMessage(south, getR3SouthItfName(), "setFloor");
            result.setErrCode(southResponse.getSetFloorResult().getErrCode());
        }
        catch (ProtocolAdapterException e)
        {
            result.setErrCode(e.getErrorCode());
        }
        return result;
    }
    
    /** 
     * 设置会场音量值
     * 
     * @param confId 会议ID
     * @param siteVolumeExs 设置会场的音量值
     * @return SDKErrorCode 0：表示成功  其他数值：表示失败
     * @see eSDK TP V100R003C20
     */
    @Override
    public SDKErrorCode setConfSiteVolumeEx(String confId,
        List<com.huawei.esdk.tp.domain.model.bean.SiteVolume> siteVolumeExs)
    {
        SDKErrorCode result = new SDKErrorCode();
        SetConfSiteVolume south = new SetConfSiteVolume();
        
        ConferenceId conferenceId = new ConferenceId();
        conferenceId.setConfInternalId(Long.parseLong(confId));
        south.setConfId(conferenceId);
        
        ArrayOfSiteVolume value = new ArrayOfSiteVolume();
        value.getSiteVolume().addAll(tpConfMgrConvert.siteVolumeExModel2Soap(siteVolumeExs));
        south.setSiteVolumes(value);
        
        try
        {
            SetConfSiteVolumeResponse southResponse =
                (SetConfSiteVolumeResponse)sendProtocolMessage(south, getR3SouthItfName(), "setConfSiteVolume");
            result.setErrCode(southResponse.getSetConfSiteVolumeResult().getErrCode());
        }
        catch (ProtocolAdapterException e)
        {
            result.setErrCode(e.getErrorCode());
        }
        return result;
    }
    
    /** 
     * 选择准备录制的视频源
     * 
     * @param confId 会议ID
     * @param videoSourceUri 视频源所在会场标识
     * @return SDKErrorCode 0：表示成功  其他数值：表示失败
     * @see eSDK TP V100R003C20
     */
    @Override
    public SDKErrorCode setRecordingVideoSourceEx(String confId, String videoSourceUri)
    {
        //        SDKErrorCode result = new SDKErrorCode();
        //        if (!StringUtils.isUintNumber(confId))
        //        {
        //            result.setErrCode(ErrInfo.CONF_ID_IS_INVALID_ERRORCODE);
        //            return result;
        //        }
        //        SetRecordingVideoSource south = new SetRecordingVideoSource();
        //        ConferenceId conferenceId = new ConferenceId();
        //        conferenceId.setConfInternalId(Long.parseLong(confId));
        //        south.setConfId(conferenceId);
        //        
        //        try
        //        {
        //            SetRecordingVideoSourceResponse southResponse =
        //                (SetRecordingVideoSourceResponse)sendProtocolMessage(south, getR3SouthItfName(), "setRecordingVideoSource");
        //            result.setErrCode(southResponse.getSetRecordingVideoSourceResult().getErrCode());
        //        }
        //        catch (ProtocolAdapterException e)
        //        {
        //            result.setErrCode(e.getErrorCode());
        //        }
        //        return result;
        //TODO wait for south implements
        return new SDKErrorCode();
    }
    
    /** 
     * 打开会议中会场的视频
     * 
     * @param confId 会议ID
     * @param siteUris 打开视频会场的URI列表
     * @return SDKErrorCode 0：表示成功  其他数值：表示失败
     * @see eSDK TP V100R003C20
     */
    @Override
    public SDKErrorCode displayConfSiteLocalVideoEx(String confId, List<String> siteUris)
    {
        SDKErrorCode result = new SDKErrorCode();
        DisplayConfSiteLocalVideo south = new DisplayConfSiteLocalVideo();
        
        ConferenceId conferenceId = new ConferenceId();
        conferenceId.setConfInternalId(Long.parseLong(confId));
        south.setConfId(conferenceId);
        
        ArrayOfString value = new ArrayOfString();
        value.getString().addAll(siteUris);
        south.setSiteUris(value);
        
        try
        {
            DisplayConfSiteLocalVideoResponse southResponse =
                (DisplayConfSiteLocalVideoResponse)sendProtocolMessage(south,
                    getR3SouthItfName(),
                    "displayConfSiteLocalVideo");
            result.setErrCode(southResponse.getDisplayConfSiteLocalVideoResult().getErrCode());
        }
        catch (ProtocolAdapterException e)
        {
            result.setErrCode(e.getErrorCode());
        }
        return result;
    }
    
    /** 
     * 关闭会议中会场的视频
     * 
     * @param confId 会议ID
     * @param siteUris 关闭视频会场的URI列表
     * @return SDKErrorCode 0：表示成功  其他数值：表示失败
     * @see eSDK TP V100R003C20
     */
    @Override
    public SDKErrorCode hideConfSiteLocalVideoEx(String confId, List<String> siteUris)
    {
        SDKErrorCode result = new SDKErrorCode();
        HideConfSiteLocalVideo south = new HideConfSiteLocalVideo();
        
        ConferenceId conferenceId = new ConferenceId();
        conferenceId.setConfInternalId(Long.parseLong(confId));
        south.setConfId(conferenceId);
        
        ArrayOfString value = new ArrayOfString();
        value.getString().addAll(siteUris);
        south.setSiteUris(value);
        
        try
        {
            HideConfSiteLocalVideoResponse southResponse =
                (HideConfSiteLocalVideoResponse)sendProtocolMessage(south,
                    getR3SouthItfName(),
                    "hideConfSiteLocalVideo");
            result.setErrCode(southResponse.getHideConfSiteLocalVideoResult().getErrCode());
        }
        catch (ProtocolAdapterException e)
        {
            result.setErrCode(e.getErrorCode());
        }
        return result;
    }
    
    @Override
    public SDKErrorCode setBroadcastContinuousPresenceEx(String confId)
    {
        SetBroadcast reqBean = new SetBroadcast();
        ConferenceId conferenceId = new ConferenceId();
        conferenceId.setConfInternalId(Long.parseLong(confId));
        reqBean.setConfId(conferenceId);
        
        String siteUri = ConfigManager.getInstance().getValue("professional.setBroadcastContinuousPresence.siteUri");
        reqBean.setSiteUri(StringUtils.isEmpty(siteUri) ? null : siteUri);

        SetBroadcastResponse res = null;
        SDKErrorCode result = new SDKErrorCode();
        try
        {
            res = (SetBroadcastResponse) sendProtocolMessage(reqBean,
                    getR3SouthItfName(),
                    "setBroadcast");
        }
        catch (ProtocolAdapterException e)
        {
            result.setErrCode(e.getErrorCode());
            return result;
        }

        if (null == res)
        {
            result.setErrCode(ErrInfo.SDK_SYSTEM_ERRORCODE);
        }
        else
        {
            result.setErrCode(res.getSetBroadcastResult().getErrCode());
        }

        return result;

    }
}
